package com.ld.admin.controller.system;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SysMenuModel;
import com.ld.admin.service.system.SysMenuService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 系统菜单
 * 
 * @ClassName SysMenuController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月09日 14:31:54
 *
 */
@Api(tags = "系统菜单")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "menu.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysMenuController")
@RequestMapping("${shieldsb.adminPath}/system/menu")
public class SysMenuController extends AdminBasicController<SysMenuModel> {
    @Autowired
    private SysMenuService service;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/menu/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/menu/sysmenu_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    // 返回json格式
    @ResponseBody
    public Object search() {
        Result result = service.getMenusData(request);
        return Web.Response.resultWithData(result);
    }

    @ApiOperation(value = "系统菜单展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/menu/sysmenu_show";
    }

    @ApiOperation(value = "跳转到修改系统菜单", notes = "")
    @RequestMapping(path = "/toUpdate", method = RequestMethod.GET)
    public String toUpdate() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/menu/sysmenu_update";
    }

    @ApiOperation(value = "获取目录下的图标", notes = "")
    @RequestMapping(path = "/geticons", method = RequestMethod.POST)
    public String geticons() {
        service.getMenuIcons(request);
        return "a/system/menu/sysmenu_icons";
    }

    @ApiOperation(value = "获取匹配的URL", notes = "")
    @RequestMapping(path = "/getUrls", method = RequestMethod.POST)
    public String getUrls() {
        service.getUrls(request);
        return "a/system/menu/sysmenu_urls";
    }

    @ApiOperation(value = "跳转到添加系统菜单", notes = "")
    @RequestMapping(path = "/toAdd", method = RequestMethod.GET)
    public String toAdd() {
        long id = getLong("id"); // 父id
        service.toAdd(request, id);
        return "a/system/menu/sysmenu_update";
    }

    @ApiOperation(value = "添加/修改系统菜单", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid SysMenuModel model) {
        Result result = service.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除系统菜单", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = service.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

}